﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenCvApplication.View
{
    public partial class FrmInput : Form
    {
        private bool isDrawing = false;
        private bool isErasing = false;
        private Bitmap bmp;
        private Point previousPoint;
        public FrmInput()
        {
            InitializeComponent();

            pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
            pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);


            // 创建一个新的 Bitmap 对象
            bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);

            // 将 Bitmap 对象赋给 PictureBox
            pictureBox1.Image = bmp;
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmInput_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
        }

        public Action<Image> Signature { get; set; }
        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Signature?.Invoke(this.pictureBox1.Image);
            this.Close();
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;
            previousPoint = e.Location;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawing)
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    // 如果处于擦除模式，则使用白色画笔，否则使用黑色画笔
                    Pen pen = new Pen(isErasing ? Color.White : Color.Black, isErasing ? 4 : 2);
                    g.DrawLine(pen, previousPoint, e.Location);
                }
                previousPoint = e.Location;
                pictureBox1.Invalidate(); // 强制 PictureBox 重新绘制
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
        }
        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            // 创建一个新的 Bitmap 对象以清除整个画布
            bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = bmp;
        }
        /// <summary>
        /// 擦除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            // 切换擦除模式
            isErasing = !isErasing;
            if (isErasing)
            {
                button6.Text = "关闭橡皮擦";
                pictureBox1.Cursor = Cursors.Hand;

            }
            else
            {
                button6.Text = "切换橡皮擦";
                pictureBox1.Cursor = Cursors.Arrow;
            }
        }
    }
}
